package com.hp.interview.design;

/**
 * TODO 注释
 *
 * @author hupan
 * @date 2021-05-20 12:27
 */
public class Calculator {

    public static void main(String[] args) {
        //表达式
        String expression = "3+20*6-20";
        //创建两个栈，一个符号栈，一个数栈
        MyArrayStack numStack = new MyArrayStack(10);
        MyArrayStack operStack = new MyArrayStack(10);
        //定义需要的相关变量
        int index = 0;//用于扫描
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;
        char ch = ' '; //每次扫描到的char保存到ch
        String keepNum = ""; //多位数
        //开始while循环的
        while (true) {
            //依次得到expression的每一个字符
            ch = expression.substring(index, index + 1).charAt(0);
            //判断ch是什么，然后再做处理
            if (operStack.isOper(ch)) //如果是运算符
            {
                if (!operStack.isEmpty())//判断当前栈是否为空
                {
                    if (operStack.priority(ch) <= operStack.priority(operStack.peek())) {
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        res = numStack.cal(num1, num2, oper);
                        //运算结果入栈
                        numStack.push(res);
                        operStack.push(ch);
                    } else {
                        //如果当前的操作符的优先级大于栈中的操作符，就直接入符号栈
                        operStack.push(ch);
                    }
                } else {
                    //如果为空，直接入符号栈
                    operStack.push(ch);
                }
            } else {
                //				//如果是数，直接入数栈
                //				numStack.push(ch - 48); //'1' + '3' = >1    (阿斯克码)

                //处理多位数
                keepNum = keepNum + ch;

                //如果ch已经是expression的最后一位，就直接入栈
                if (index == expression.length() - 1) {
                    numStack.push(Integer.parseInt(keepNum));
                } else {

                    //判断下一个是不是数字，如果是数字，就继续扫描，运算符啊，则入栈
                    if (operStack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
                        numStack.push(Integer.parseInt(keepNum));
                        keepNum = "";  //置空
                    }
                }

            }

            //让index+1，并判断是否扫描到expression最后
            index++;
            if (index >= expression.length()) {
                break;
            }
        }

        //当表达式扫描完毕，就顺序的从数栈 + 符号栈中pop相应的数和符号，并运行
        while (true) {
            if (operStack.isEmpty()) {
                break;
            }
            //如果符号栈为空，则计算到最后结果
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            res = numStack.cal(num1, num2, oper);
            numStack.push(res); //入栈
        }
        //将数栈的最后数，pop;

        System.out.printf("表达式%s = %d", expression, numStack.pop());
    }
}

//定义一个ArrayStack表示栈
class MyArrayStack {
    private int maxSize;  //最大容量
    private int[] stack;  //数组模拟栈，数据就放在该数组中
    private int top = -1; //top表示栈顶，初始化为-1

    //构造器
    public MyArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }

    //增加一个方法，可以返回当前栈顶的值，但不是真正的pop
    public int peek() {
        return stack[top];
    }

    //判断是否栈满
    public boolean isFull() {
        return top == maxSize - 1;
    }

    //判断是否栈空
    public boolean isEmpty() {
        return top == -1;
    }

    //入栈
    public void push(int value) {
        if (isFull()) {
            System.out.println("栈已满~");
            return;
        }
        top++;
        stack[top] = value;
    }

    //出栈
    public int pop() {
        if (isEmpty())
            throw new RuntimeException("栈已空，无数据~");
        int value = stack[top];
        top--;
        return value;
    }

    //显示栈的情况，遍历栈
    public void list() {
        if (isEmpty()) {
            System.out.println("栈空，没有数据~");
            return;
        }
        for (int i = top; i >= 0; i--) {
            System.out.printf("stack[%d]=%d\n", i, stack[i]);
        }
    }

    //返回运算器的优先级，数字越大，优先级越高
    public int priority(int oper) {
        if (oper == '*' || oper == '/') {
            return 1;
        } else if (oper == '+' || oper == '-') {
            return 0;
        } else
            return -1;   //假定目前表达式只有
    }

    //判断是不是运算符
    public boolean isOper(char val) {
        return val == '+' || val == '-' || val == '*' || val == '/';
    }


    //计算方法
    public int cal(int num1, int num2, int oper) {
        int res = 0;  //用于存放结果
        switch (oper) {
            case '+':
                res = num2 + num1;
                break;
            case '-':
                res = num2 - num1;
                break;
            case '*':
                res = num2 * num1;
                break;
            case '/':
                res = num2 / num1;
                break;
            default:
                break;
        }
        return res;
    }
}